package com.zxw.hotel.api;

import com.github.pagehelper.PageInfo;
import com.zxw.hotel.common.Pageinfo;
import com.zxw.hotel.model.Reserve;
import com.zxw.hotel.model.extend.ReserveExtend;
import com.zxw.hotel.service.ReserveService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/reserve")
public class ReserveApi {
    private ReserveService reserveService;
    @Autowired
    public ReserveApi(ReserveService reserveService) {
        this.reserveService = reserveService;
    }
    @GetMapping
    //查询
    public Map<String,Object> getall(@RequestParam(defaultValue ="1") Integer pageNo,
                                     @RequestParam(defaultValue = "2147483647") Integer pageSize,
                                     ReserveExtend reserveExtend){
        if(pageNo == null || pageNo < 1 ){
            pageNo = 1;
        }
        if(pageSize == null || pageSize < 1 ){
            pageSize = Integer.MAX_VALUE;
        }
        Pageinfo pageinfo = new Pageinfo(pageNo,pageSize);
        List<Reserve> allreserves = reserveService.getAll(pageinfo,reserveExtend);
        PageInfo<Reserve> pi = new PageInfo<>(allreserves);
        Map<String,Object> map = Map.of("total",pi.getTotal(),"pageNo",pi.getPageNum(),"pageSize",pi.getPageSize());
        return Map.of("success",true,"data",allreserves,"pi",map);
    }
    //删除
    @DeleteMapping
    public ResponseEntity<Map<String,Object>> delete(@RequestBody Integer[] ids){
        int delete = reserveService.delete(ids);
        return ResponseEntity.ok().body(Map.of("success",delete>0));
    }
    //修改 输入 客户姓名 和 房间编号
    @PutMapping
    public ResponseEntity<Map<String,Object>> update(@RequestBody ReserveExtend reserveExtend){
        int update = reserveService.update(reserveExtend);
        return ResponseEntity.ok().body(Map.of("success",update>0));
    }
    //新增 输入userid roomid
    @PostMapping
    public ResponseEntity<Map<String,Object>> insert(@RequestBody ReserveExtend reserveExtend){
        int insert = reserveService.insert(reserveExtend);
        return ResponseEntity.ok().body(Map.of("success",insert>0));
    }


}
